/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.yixiang.co
* 注意：
* 本软件为www.yixiang.co开发研制，未经购买不得使用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package co.yixiang.modules.auction.service.mapper;

import co.yixiang.common.mapper.CoreMapper;
import co.yixiang.modules.auction.domain.YxAuctionOrder;
import co.yixiang.modules.auction.service.dto.YxAuctionOrderConfirm;
import co.yixiang.modules.auction.service.dto.YxAuctionOrderList;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
* @author yhh
* @date 2020-09-21
*/
@Repository("YxAuctionOrderMapper")
public interface YxAuctionOrderMapper extends CoreMapper<YxAuctionOrder> {

    @Select("SELECT ao.id,ar.price,ap.slide_img as 'productImg',ap.image,ap.title as 'productName' from yx_auction_order ao " +
            " LEFT JOIN yx_auction_record ar on ao.auction_record_id=ar.id" +
            " LEFT JOIN yx_auction_product ap on ar.auction_product_id=ap.id" +
            " where ao.id=#{id} and  ao.is_del=0 and ao.status=2 and ao.user_id=#{uid}")
    YxAuctionOrderConfirm confirmOrder(@Param("uid") Long uid, @Param("id")Long id);

    @Select("<script> SELECT ao.id,ar.price,ao.is_max,ao.status,ap.slide_img as 'productImg',ap.image,ap.title as 'productName'  ," +
            " ar.start_time as cTime , ac.start_time,ac.end_time, ao.create_time as payTime  " +
            " from yx_auction_order ao" +
            " LEFT JOIN yx_auction_record ar on ao.auction_record_id=ar.id " +
            " LEFT JOIN yx_auction_config ac on ac.id=ar.time_id " +
            " LEFT JOIN yx_auction_product ap on ar.auction_product_id=ap.id" +
            " where  ao.status=#{type} and ao.user_id=#{uid} and ao.is_del =0 order by ao.id desc </script> ")
    Page<YxAuctionOrderList> productOrderPage(@Param("page") Page page1,@Param("uid") Long uid, @Param("type")Integer type);

    @Select("SELECT ao.id,ar.price,ao.status,ap.slide_img as 'productImg',ap.image,ap.title as 'productName' from yx_auction_order ao " +
            " LEFT JOIN yx_auction_record ar on ao.auction_record_id=ar.id" +
            " LEFT JOIN yx_auction_product ap on ar.auction_product_id=ap.id" +
            " where  ao.status=4 and ao.receive_type=#{type} and ao.user_id=#{uid} and ao.is_del =0 order by ao.update_time desc")
    Page<YxAuctionOrderList> productHistoryOrderPage(Page page1, Long uid, Integer type);


    @Select("SELECT id FROM yx_auction_order where `status` = #{status} and receive_type = #{receiveType} and  DATE_ADD(pay_time, INTERVAL 7 DAY) <= NOW()")
    List<String> getNotOperatingOrderList(@Param("receiveType") Integer receiveType, @Param("status") Integer status);

    @Update("UPDATE yx_auction_order set status = #{status} where id in (${strOIds})")
    void updateActionOrderStatus(@Param("status") Integer status,@Param("strOIds") String strOIds);

    @Select("select * from yx_auction_order where auction_record_id=#{id} and status !=5 and is_del=0")
    YxAuctionOrder selectByAuctionRecord(Long id);


    @Select("select count(1) from yx_auction_order where user_id=#{uid} and is_del=0 and status !=4 and status !=5")
    Integer selectCountByUid(Long uid);

    @Select("SELECT sum(price) from yx_auction_order  where user_id=#{uid} and is_del=0 and status !=5 and status !=0 and TO_DAYS(create_time)=TO_DAYS(now()) ")
    BigDecimal selectDayMoneyByUid(Long uid);

    @Select("SELECT sum(price) from yx_auction_order  where user_id=#{uid} and is_del=0 and status !=5 and status !=0 and TO_DAYS(create_time)<=TO_DAYS(now()) and TO_DAYS(create_time)>=TO_DAYS(now())-7")
    BigDecimal selectWeekMoneyByUid(Long uid);

    @Select("SELECT sum(price) from yx_auction_order  where user_id=#{uid} and is_del=0 and status !=5 and status !=0 and pay_time>'2020-11-01' ")
    BigDecimal selectAddMoneyByUid(Long uid);

    @Select("<script>  SELECT ao.id,ar.price,ao.is_max,ao.status,ap.slide_img as 'productImg',ap.image,ap.title as 'productName' ," +
            " ar.start_time as cTime , ac.start_time,ac.end_time as paiTime , ao.create_time as payTime  " +
            " from yx_auction_order ao " +
            " LEFT JOIN yx_auction_record ar on ao.auction_record_id=ar.id" +
            " LEFT JOIN yx_auction_config ac on ac.id=ar.time_id " +
            " LEFT JOIN yx_auction_product ap on ar.auction_product_id=ap.id" +
            " where  (ao.status=3 or ao.status=-3) and ao.user_id=#{uid} and ao.is_del =0 order by ao.id desc </script> ")
    Page<YxAuctionOrderList> productOrder1Page(Page page1, Long uid, Integer type);

    @Select("select count(1) from yx_auction_order where user_id=#{uid} and is_del=0  and status !=5 and to_days(create_time) = to_days(now())")
    Integer selectTodayCountByUid(@Param("uid") Long uid);

    @Select("select count(1) from yx_auction_order where user_id=#{uid} and is_del=0 and to_days(create_time) = to_days(now())")
    Integer selectTodayCountByUid1(@Param("uid") Long uid);

    @Select("select IFNULL(SUM(price),0) from yx_auction_order where user_id=#{uid} and is_del=0  and status !=5 and to_days(create_time) = to_days(now())")
    BigDecimal selectTodayPriceByUid(@Param("uid") Long uid);

    @Select(" SELECT * from yx_auction_order where DATE_FORMAT(create_time,'%Y-%m-%d') = DATE_FORMAT(now(),'%Y-%m-%d') and status = #{status} ")
    List<YxAuctionOrder> selectAuctionOrderList(@Param("status") Integer status);

    @Select("SELECT COUNT(DISTINCT(user_id)) from yx_auction_order where DATE_FORMAT(create_time,'%Y-%m-%d') = DATE_FORMAT(now(),'%Y-%m-%d') and status = #{status}")
    Integer getQdNum(@Param("status") Integer status);

    @Update( "UPDATE yx_user set now_money = (now_money - #{serviceMoney}) where uid = #{uid}" )
    void updateUserPrice(@Param("uid") Long uid,@Param("serviceMoney") BigDecimal serviceMoney);

    @Select(" SELECT COUNT(1) from yx_auction_user_log where uid = #{uid} and TO_DAYS(auction_time) = TO_DAYS(now()) ")
    Integer selectUserLog(@Param("uid") Long uid);

    @Insert(" INSERT into yx_auction_user_log (uid , auction_time) VALUES (#{uid} , NOW())  ")
    void addUserLog(@Param("uid") Long uid);

    @Select( "SELECT COUNT(1) from yx_auction_user_log where TO_DAYS(auction_time) = TO_DAYS(now()) " )
    Integer getQpNum();

    @Select( "SELECT COUNT(1) from yx_auction_user_log where TO_DAYS(auction_time) = TO_DAYS(now()) and is_success = 0 " )
    Integer getWqdNum();

    @Select(" SELECT url from yx_auction_people where id = 1 ")
    String getNewPeople();

    @Update(" update yx_user set now_money = #{balance} , data_version = data_version + 1 where uid = #{uid} ")
    Integer updateUserPriceById(@Param("uid") Long uid,@Param("balance") BigDecimal balance);

    @Select("SELECT create_time from yx_auction_order where user_id = #{uid} and TO_DAYS(create_time) = TO_DAYS(now()) ORDER BY create_time DESC limit 1")
    Date getFinalOrderTime(Long uid);


    @Select(" SELECT title from yx_auction_member where id = ( SELECT link_id from yx_user_bill where uid = #{uid} and title = '开通会员' and `status` = 1 order by create_time DESC limit 1 ) ")
    String selectMemberName(Long uid);


    @Select(" SELECT COUNT(1) from yx_user where create_time > DATE_SUB(now(),INTERVAL (SELECT memo from yx_appdict where code = 'newPeople') DAY) and uid = #{uid} ")
    Integer isNewPeople(Long uid);
}
